############## Austerity Social Spending Replication ##########################

###### PREPARE: RUN lp_builder and plot_lp functions (see other files) ####

setwd("...")

macro_data <- read_rds("as_macro_data.RData")

### see package dependencies in previous files

library(lpirfs) # Adämmer reference package
library(tidyverse)
library(stargazer)
library(lmtest)
library(plm)
library(ggpubr)

###### Estimation 

macro_lp <- as.data.frame(macro_data) # define dataset

### Validation with package from Adämmer 

# lpirfs from Adämmer 

lp1 <- lp_lin_panel(data_set = macro_lp,
                    endog_data = "socexp_public",
                    cumul_mult = TRUE, shock = "SPENDING",
                    c_exog_data = c("unemp", "realgdpgr"),
                    l_exog_data = c("unemp","realgdpgr"),
                    lags_exog_data = 2, diff_shock = F,
                    panel_model = "within",
                    panel_effect = "individual",
                    robust_cov = "vcovBK", confint = 1.96,
                    hor = 8)

plot(lp1)

lp1$reg_summaries

# baseline model

lpb_b <- lp_builder(data = macro_lp, panelid = c("country","year"),
                    endog = "socexp_public", shock = "SPENDING",
                    hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                    model = "within", effect = "individual",
                    c_exog = c("realgdpgr","unemp"),
                    lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb_b$reg_summary


############### BASELINE RESULTS ##############################################

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = T, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", 
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp","SPENDING"),
                   l_lags = 2, lead_exog = c("SPENDING"))

(lpb2$reg_result[[1]][1] + lpb2$reg_result[[2]][1] + lpb2$reg_result[[3]][1] + lpb2$reg_result[[4]][1] + lpb2$reg_result[[5]][1] + lpb2$reg_result[[6]][1] + lpb2$reg_result[[7]][1] + lpb2$reg_result[[8]][1]) / 8

p01 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, lims = c(-1.9,0.175)) +
  theme_pubclean() +
  theme(text = element_text(family="Times New Roman"),
        axis.line = element_line(color="grey", linewidth = 0.6),
        axis.ticks = element_line(color="grey"))

p02 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.9,0.175)) +
  theme_pubclean() +
  theme(text = element_text(family="Times New Roman"),
        axis.line = element_line(color="grey", linewidth = 0.6),
        axis.ticks = element_line(color="grey"))

ggsave(plot = p01, filename = "./Main Figures/IRF_FIG01.png", dpi = 800, height = 3, width = 3.5)
ggsave(plot = p02, filename = "./Main Figures/IRF_FIG02.png", dpi = 800, height = 3, width = 3.5)



##################### CONSUMPTION VS. INVESTMENT? ##############################

########### Main Spending Categories

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "cons_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "insurance_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "labmarket_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb4 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socinvest_spending1", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb5 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socinvest_spending2", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb6 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "invest_spending", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.65,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.2,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p3 <- plot_lp(lpb3, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.2,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p4 <- plot_lp(lpb4, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-0.5,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p5 <- plot_lp(lpb5, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.2,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p6 <- plot_lp(lpb6, hor = 8, col = "blue", pointest = T, ribbon = T, lims = c(-1.2,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "./Main Figures/IRF_FIG04.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p2, filename = "./Main Figures/IRF_FIG05.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p3, filename = "./Main Figures/IRF_FIG06.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p4, filename = "./Main Figures/IRF_FIG07.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p5, filename = "./Main Figures/IRF_FIG08.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p6, filename = "./Main Figures/IRF_FIG09.png", dpi = 600, height = 3, width = 3.5)


####### fully decomposed social spending #####

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "oldage_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "health_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp","elderly"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "educexp_public_ipol", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual", 
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb4 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "family_inkind_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb5 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "gfcf_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

lpb6 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "rd_exp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"), l_lags = 2)

p1 <- plot_lp(lpb1, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.7,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p2 <- plot_lp(lpb2, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.7,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p3 <- plot_lp(lpb3, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.7,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p4 <- plot_lp(lpb4, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.15,0.1)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p5 <- plot_lp(lpb5, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.7,0.2)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))
p6 <- plot_lp(lpb6, hor = 8, col = "blue", pointest = T, ribbon = T) + scale_y_continuous(limits = c(-0.02,0.02)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), axis.ticks = element_line(color="grey"))

ggsave(plot = p1, filename = "./Main Figures/IRF_FIG_oldage.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p2, filename = "./Main Figures/IRF_FIG_health.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p3, filename = "./Main Figures/IRF_FIG_educ.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p4, filename = "./Main Figures/IRF_FIG_faminkind.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p5, filename = "./Main Figures/IRF_FIG_gfcf.png", dpi = 600, height = 3, width = 3.5)
ggsave(plot = p6, filename = "./Main Figures/IRF_FIG_rd.png", dpi = 600, height = 3, width = 3.5)



#################### WHEN IS RETRENCHMENT POSSIBLE? ############################

macro_lp_78_18 <- macro_lp %>% dplyr::filter(year>=1978 & year<=2018)

tapply(macro_lp_78_18$SPENDING, macro_lp_78_18$fincrisis, FUN = "sum", na.rm=T)

tapply(macro_lp_78_18$SPENDING, macro_lp_78_18$fincrisis2, FUN = "sum", na.rm=T)

63.39614/(74.68755 + 63.39614)

73.67557/(64.40811 + 73.67557)

table(macro_lp_78_18$fincrisis)

# around 46% of fiscal adjustment occurs in the year of or 5 years after a financial crisis
# around 53% of fiscal adjustment occurs in the 6 years after a financial crisis

# ONLY STANDARD LP ESTIMATION: 

lpb1 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "fincrisis")

p1 <- plot_lp(lpb1, hor = 8, col1 = NULL, col2 = NULL,
              pointest = T, statedep = TRUE,
              statelabs = "Financial Crisis",
              ribbon = T, lty1 = NULL, lty2 = NULL,
              estimat = "within", lims = c(-2.6,0.75)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p1, filename = "./Main Figures/IRF_FIG10.png", dpi = 800, height = 3, width = 2.5)


## Deficit ##

tapply(macro_lp_78_18$SPENDING, macro_lp_78_18$deficit_bin, FUN = "sum", na.rm=T)

lpb2 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "deficit_bin")

p2 <- plot_lp(lpb2, hor = 8, col1 = NULL, col2 = NULL, pointest = T, statedep = TRUE,
              statelabs = "High Deficits", ribbon = T, lty1 = NULL,
              lty2 = NULL, estimat = "within", lims = c(-2.6,0.75)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p2, filename = "./Main Figures/IRF_FIG11.png", dpi = 800, height = 3, width = 2.5)

## Slump ##

tapply(macro_lp_78_18$SPENDING, macro_lp_78_18$recession, FUN = "sum", na.rm=T)

lpb3 <- lp_builder(data = macro_lp, panelid = c("country","year"),
                   endog = "socexp_public", shock = "SPENDING",
                   hor = 8, cum_mult = TRUE, vcov_mat = "vcovBK",
                   model = "within", effect = "individual",
                   c_exog = c("realgdpgr","unemp"),
                   lag_exog = c("realgdpgr","unemp"),
                   l_lags = 2, statedep = TRUE, statevar = "recession")

p3 <- plot_lp(lpb3, hor = 8, col1 = NULL, col2 = NULL, pointest = T, statedep = TRUE,
              statelabs = "Recession", ribbon = T, lty1 = NULL, lty2 = NULL, estimat = "within", lims = c(-2.6,0.75)) +
  theme(panel.border = element_rect(fill=NA, linewidth=1.2, color="grey"), legend.position = "bottom")

ggsave(plot = p3, filename = "./Main Figures/IRF_FIG12.png", dpi = 800, height = 3, width = 2.5)


